<?php

namespace Apps\Manage\Controllers;


use Apps\Common\Models\QuestionTag;
use PhalconPlus\Common\ResponseResult;
use PhalconPlus\Common\ResponseResultStatus;
/**
 * 问题标签1.0,2.0版本没用
 * @author     chenxiaolin
 * @date        2016年1月19日
 * @version     2.0.0
 * @copyright   Copyright 2016 meelier.com
 */
class QuestiontagController extends BaseController
{
    public function initialize()
    {
        parent::initialize();
        $this->setMainNav('question');
        $this->setLeftNav('tag');
    }

    public function indexAction()
    {
        $this->response->redirect('questiontag/list');
    }

    public function listAction()
    {
        $this->view->setVar('data', QuestionTag::find());
    }

    public function setStateAction()
    {
        $req = $this->request;
        if($req->isPost()) {
            $id = intval($req->getPost('id', null, 0));
            $state = intval($req->getPost('state', null, -10000));
            if(!in_array($state, [-1, 0, 1])) {
                return (new ResponseResult())->sendError(ResponseResultStatus::PARAM_CANNOT_EMPTY, '请求异常');
            }
            $info = QuestionTag::findFirst('tag_id=' . $id);
            if($info) {
                $info->update(['tag_state' => $state]);
            }
            return (new ResponseResult())->sendResult('ok');
        } else {
            return (new ResponseResult())->sendError(ResponseResultStatus::PARAM_CANNOT_EMPTY, '请求异常');
        }
    }

    public function updateAction()
    {
        $req = $this->request;
        if(!$req->isPost()) {
            $id = intval($req->getQuery('id', null, 0));
            $tagTitle = '';
            $tagOrder = 0;
            if($id > 0) {
                $info = QuestionTag::findFirst('tag_id=' . $id);
                if($info) {
                    $tagTitle = $info->tag_title;
                    $tagOrder = $info->tag_order;
                }
            }
            $this->view->setVar('id', $id);
            $this->view->setVar('tagTitle', $tagTitle);
            $this->view->setVar('tagOrder', $tagOrder);
            return;
        }
        $response = new ResponseResult();
        $response->callback = $req->getPost('callback', null, 'parent.setFormResult');
        $response->callbackJavascriptTag = true;

        $id = intval($req->getPost('tag_id', null, 0));
        $title = $req->getPost('tag_title');
        $order = intval($req->getPost('tag_order', null, '-10000'));
        // 通过名称查找，是否存在
        $exist = function($title, $id) {
            $existInfo = QuestionTag::query()
                ->columns('*')
                ->where('tag_title = :title:', ['title' => $title])
                ->limit(1)->execute()->getFirst();
            if($existInfo && $existInfo->tag_id != $id) {
                return true;
            }
            return false;
        };
        if($id > 0) { // 修改
            $info = QuestionTag::findFirst('tag_id=' . $id);
            if (!$info) {
                $response->sendError(ResponseResultStatus::ERROR, '分类不存在！');
                return $response;
            }
            if($exist($title, $id)) {
                $response->sendError(ResponseResultStatus::ERROR, '名称已经存在了！');
                return $response;
            }
            $data = [];
            if ($info->tag_title != $title) {
                $data['tag_title'] = $title;
            }
            if ($order > -1) {
                $data['tag_order'] = $order;
            }
            if (count($data) > 0) {
                $info->update($data);
            }
        } else { // 添加
            if($exist($title, $id)) {
                $response->sendError(ResponseResultStatus::ERROR, '名称已经存在了！');
                return $response;
            }

            $tag = new QuestionTag();
            $tag->tag_title = $title;
            $tag->tag_order = $order;
            if(!$tag->save()) {
                $this->databaseErrorLog($tag);
                $response->sendError(ResponseResultStatus::DATABASE_ERROR, '保存数据异常！');
                return $response;
            }
        }
        $response->sendResult('ok');
        return $response;
    }
}